package cn.zr.aitool.view;

import android.app.DatePickerDialog;
import android.app.Dialog;
import android.content.Context;
import android.text.TextUtils;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.DatePicker;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.RadioGroup;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

import cn.zr.aitool.Constants;
import cn.zr.aitool.R;
import cn.zr.aitool.db.ContentManager;
import cn.zr.aitool.bean.ContentBean;
import cn.zr.aitool.factory.ModelFactory;
import cn.zr.aitool.model.IContentModel;


public class EidtDialog extends Dialog {
    private static final String TAG = "EidtDialog";
    private static final String TYPE_START = "typeStart";
    private static final String TYPE_END = "typeEnd";
    private TextView mStartTime;
    private TextView mEndTime;
    private Context mContext;
    private String mCurrentType;
    private EditText mTitle;
    private EditText mContentEt;
    private Button mCommit;
    private OnCommitListener onCommitListener;
    private int position;
    private EditText mContentEt1;
    private EditText mContentEt2;
    private RadioGroup mRadioGroup;

    private RadioButton mRbtnBuy;
    private RadioButton mRbtnCeil;

    public interface OnCommitListener {
        void onCommit();
    }

    /**
     * @param context
     */
    public EidtDialog(Context context, int position) {
        super(context, R.style.Theme_AppCompat_Dialog_Alert);
        View dialogView = View.inflate(context, R.layout.dialog_eidt, null);
        getWindow().setWindowAnimations(R.style.DialogOutAndInStyle);
        setContentView(dialogView);
        mContext = context;
        this.position = position;
        mTitle = dialogView.findViewById(R.id.title);
        mContentEt = dialogView.findViewById(R.id.content);
        mContentEt1 = dialogView.findViewById(R.id.content_1);
        mContentEt2 = dialogView.findViewById(R.id.content_2);
        mStartTime = dialogView.findViewById(R.id.start_time);
        mEndTime = dialogView.findViewById(R.id.end_time);
        mCommit = dialogView.findViewById(R.id.commit);
        mRadioGroup = dialogView.findViewById(R.id.rg_home);
        mRbtnBuy = dialogView.findViewById(R.id.rb_buy);
        mRbtnCeil = dialogView.findViewById(R.id.rb_ceil);
        mStartTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showTime(TYPE_START);
            }
        });
        mEndTime.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                showTime(TYPE_END);
            }
        });
        mCommit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                insert();
            }
        });
        initHint();
        initRadioGroup();
    }

    private void initRadioGroup() {
        mRadioGroup.setVisibility(View.GONE);
        switch (position) {
            case Constants.POSITION_GUPIAO:
                mRadioGroup.setVisibility(View.VISIBLE);
                mRbtnBuy.setText("买入");
                mRbtnCeil.setText("卖出");
                break;
            case Constants.POSITION_HOUSE:
                mRadioGroup.setVisibility(View.VISIBLE);
                mRbtnBuy.setText("支出");
                mRbtnCeil.setText("收入");
                break;
            default:
        }

    }

    private void initHint() {
        IContentModel model = ModelFactory.getEditModel(position);
        mTitle.setHint(model.getContentHint().get(0));
        mContentEt.setHint(model.getContentHint().get(1));
        mContentEt1.setHint(model.getContentHint().get(2));
        mContentEt2.setHint(model.getContentHint().get(3));
    }

    private void insert() {
        IContentModel eidtModel = ModelFactory.getEditModel(position);
        ContentBean model = new ContentBean();
        model.title = eidtModel.getContentHint().get(0) + ":" + mTitle.getText().toString();
        model.content = eidtModel.getContentHint().get(1) + ":" + mContentEt.getText().toString();

        model.start = mStartTime.getText().toString();
        model.end = mEndTime.getText().toString();
        if (TextUtils.isEmpty(model.start)) {
            Calendar calendar = Calendar.getInstance();
            SimpleDateFormat formatter = new SimpleDateFormat("yyyy-MM-dd");
            model.start = formatter.format(new Date(calendar.getTimeInMillis()));
        }
        model.position = this.position;
        model.type = getTypeByCheckId();
        model.content1 = eidtModel.getContentHint().get(2) + ":" + mContentEt1.getText().toString();
        model.content2 = eidtModel.getContentHint().get(3) + ":" + mContentEt2.getText().toString();
        ContentManager.getInstance().insertGroup(model.toContentValues());
        if (onCommitListener != null) {
            onCommitListener.onCommit();
        }
        dismiss();
    }

    private int getTypeByCheckId() {
        return mRadioGroup.getCheckedRadioButtonId() == R.id.rb_buy ? 0 : 1;
    }

    public OnCommitListener getOnCommitListener() {
        return onCommitListener;
    }

    public void setOnCommitListener(OnCommitListener onCommitListener) {
        this.onCommitListener = onCommitListener;
    }

    private void showTime(String type) {
        mCurrentType = type;
        //获取当前年月日
        Calendar calendar = Calendar.getInstance();
        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);

        int day = calendar.get(Calendar.DAY_OF_MONTH);
        //new一个日期选择对话框的对象,并设置默认显示时间为当前的年月日时间.
        DatePickerDialog dialog = new DatePickerDialog(mContext, mdateListener, year, month, day);
        dialog.show();
    }

    /**
     * 日期选择的回调监听
     */
    private DatePickerDialog.OnDateSetListener mdateListener = new DatePickerDialog.OnDateSetListener() {

        @Override
        public void onDateSet(DatePicker view, int years, int monthOfYear, int dayOfMonth) {
            Log.i(TAG, "年" + years + "月" + monthOfYear + "日" + dayOfMonth);
            StringBuilder time = new StringBuilder();
            time.append(years).append("-").append(monthOfYear + 1).append("-").append(dayOfMonth).append("-");
            if (mCurrentType.equals(TYPE_START)) {
                mStartTime.setText(time.toString());
            } else {
                mEndTime.setText(time.toString());
            }
        }
    };

    public String getEndTime() {
        return mEndTime.getText().toString();
    }

    public String getStartTime() {
        return mStartTime.getText().toString();
    }

    public String getTitle() {
        return mTitle.getText().toString();
    }

    public String getContent() {
        return mContentEt.getText().toString();
    }


    @Override
    public void show() {
        super.show();
    }

    @Override
    public void dismiss() {
        super.dismiss();
    }
}
